<?php
class v1_site{
    function index(){
    	$data['basic']=$this->yum->config['Manage']['domain'].'?c=site&a=index';
		$params=array('limit'=>array(intval($this->yum->get('start')),15));
		if($this->yum->get('order',true)){
			$data['order']=$this->yum->get('order',true);$data['rule']=$this->yum->get('rule',true);
			if(in_array($data['order'],array('pr','alexa','date'))&&in_array($data['rule'],array('desc','asc'))){
				$params['order'][$data['order']]=$data['rule'];
			}
		}
		if($this->yum->get('date',true)){
			$data['date']=explode(',',$this->yum->get('date',true));
			if(count($data['date'])==2){
				$params['where']=array('date >='=>strtotime($data['date'][0]),'date <='=>strtotime($data['date'][1]));
				$data['basic'].='&date='.$data['date'][0].','.$data['date'][1];
			}
		}
		if($this->yum->get('search',true)){
			$data['search']=urldecode($this->yum->get('search',true));$data['tab']=$this->yum->get('tab',true);
			if($data['search']!=''){
				if($data['tab']=='domain'){
					$params['where']['domain LIKE']='%'.$data['search'].'%';
				}
				$data['basic'].='&tab='.$data['tab'];
				$data['basic'].='&search='.urlencode($data['search']);
			}
		}
    	$site=$this->yum->model('site');
    	$data['total']=$site->GSite(array('select'=>'COUNT(*) AS count','where'=>$params['where']));
    	$data['total']=empty($data['total'])?0:$data['total'][0]['count'];
		$data['site']=$site->GSite($params);
		$pagination=$this->yum->library('pagination',array('query_string'=>'start','base_url'=>$data['basic'].'&order='.$data['order'].'&rule='.$data['rule'],'total_rows'=>$data['total'],'per_page'=>15,'first_link'=>'首页','last_link'=>'末页','cur_tag_open'=>'<a href="#" class="active">','cur_tag_close'=>'</a>'));
		$data['pagination']=$pagination->create_links();
		$this->yum->set($data);
		$this->yum->view('index.php');
    }
	function edit(){
		$site=$this->yum->model('site');
		if($this->yum->post('save')){
			$output=array('result'=>0,'msg'=>'');
			$id=intval($this->yum->post('site',true));$domain=$this->yum->post('domain',true);
			$pr=intval($this->yum->post('pr',true));$alexa=intval($this->yum->post('alexa',true));
			if(preg_match("/(^http(s)*:\/\/[a-zA-Z0-9]+(\.[a-zA-Z0-9]+)+)(\/.*)*$/u",$domain,$match)==0){
				$output['msg']='请输入一个正确的网址,注意需要包含http(s)*://';
			}else{
				$domain=$match[1];
				$affected=$site->USite(array('where'=>array('id='=>$id),'set'=>array('domain='=>$domain,'pr='=>$pr,'alexa='=>$alexa)));
				if($affected==1){
					$output['result']=2;
					$output['msg']='修改成功.';
				}else{
					$output['msg']='请误提交相同或未经修改的数据.';
				}
			}
			header('Content-Type:text/plain');
			echo json_encode($output);
		}else{
			$current=intval($this->yum->get('site',true));
			$data['site']=$site->GSite(array('where'=>array('id ='=>$current)));
			$data['site']=reset($data['site']);
			$this->yum->set($data);
			$this->yum->view('edit.php');
		}
	}
	function wipe(){
		$output=array('result'=>0,'msg'=>'');
		$site=$this->yum->model('site');
		$id=$this->yum->get('site');
		if(is_array($id)){
			$affected=$site->DSite(array('where'=>array('id IN'=>$id)));
			$output['result']=2;
		}else{
			$affected=$site->DSite(array('where'=>array('id='=>$id)));
			if($affected==1){
				$output['result']=1;
			}else{$output['msg']='请求有误.';}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
	function update(){
		$output=array('result'=>0,'msg'=>'','offset'=>0,'data'=>array());
		header('Content-type:application/json');
		$current=intval($this->yum->get('site',true));
		$site=$this->yum->model('site');
		$current=$site->GSite(array('where'=>array('id ='=>$current)));
		if(empty($current)){
			$output['msg']='错误的请求数据.';
		}else{
			$current=reset($current);
			$seo=$this->yum->library('seo');
			$pr=$seo->GoogleRank($current['domain']);
			$alexa=$seo->AlexaRank($current['domain']);
			$affected=$site->USite(array('where'=>array('id='=>$current['id']),'set'=>array('pr='=>$pr,'alexa='=>$alexa)));
			if($affected==1){
				$output['result']=5;
				$output['td']=array('offset'=>3,'data'=>array($pr,$alexa));
			}else{$output['msg']=$current['domain'].':PR及Alexa已是最新.';}
		}
		header('Content-Type:text/plain');
		echo json_encode($output);
	}
}
?>